package com.ruoyi.hxy.task;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.hxy.dto.goods.HxyGoodsSkuResDto;
import com.ruoyi.hxy.service.IHxyGoodsService;
import com.ruoyi.hxy.service.IHxyOrderService;
import com.ruoyi.xp.order.dto.OrderDto;
import com.ruoyi.xp.order.dto.OrderIncomeDto;
import com.ruoyi.xp.order.dto.OrderItemDto;
import com.ruoyi.xp.order.dto.OrderItemInfoDto;
import com.ruoyi.xp.order.service.IOrderInfoService;
import com.ruoyi.xp.supply.dto.SupplyWarehouseGoodsDto;
import com.ruoyi.xp.supply.service.ISupplyWarehouseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chendx
 */
@Component("HxyTask")
public class HxyTask {

    private static final Logger log = LoggerFactory.getLogger(HxyTask.class);

    @Autowired
    private IOrderInfoService orderInfoService;

    @Autowired
    private ISupplyWarehouseService supplyWarehouseService;

    @Autowired
    private IHxyOrderService hxyOrderService;

    @Autowired
    private IHxyGoodsService hxyGoodsService;

    /**
     * 同步货小易订单商品明细信息
     */
    public void syncHxyOrderItem() {

        log.info("开始同步货小易订单");

        List<OrderDto> orderDtoList = ObjectUtil.isNull(orderInfoService.getOrderHaveNoItem()) ? new ArrayList<>() : orderInfoService.getOrderHaveNoItem();
        for (OrderDto orderDto : orderDtoList) {
            OrderItemInfoDto orderItemInfoDto = null;
            try {
                orderItemInfoDto = hxyOrderService.getOrderItemByOrderSn(orderDto.getOrderSn());
                orderInfoService.addOrderItemInfo(orderItemInfoDto);
            } catch (IOException e) {
                log.error("获取货小易{}订单失败", orderDto.getOrderSn(), e);
            }
        }

        log.info("结束同步货小易订单");
    }


    /**
     * 同步货小易订单商品明细信息
     */
    public void syncHxyOrderItemGoodsOrHeadPackage() {

        log.info("开始同步货小易订单商品明细出货信息");

        List<OrderItemDto> orderItemDtoList = ObjectUtil.isNull(orderInfoService.getOrderItemHaveNoGoodsOrHeadPackage()) ? new ArrayList<>() : orderInfoService.getOrderItemHaveNoGoodsOrHeadPackage();
        for (OrderItemDto orderItemDto : orderItemDtoList) {
            OrderItemInfoDto orderItemInfoDto = null;
            try {
                orderItemInfoDto = hxyOrderService.getOrderItemByOrderSn(orderItemDto.getOrderSn());
                orderInfoService.addOrderItemGoodsOrHeadPackageInfo(orderItemInfoDto);
            } catch (IOException e) {
                log.error("获取货小易{}订单失败", orderItemDto.getOrderSn(), e);
            }
        }

        log.info("结束同步货小易订单商品明细出货信息");
    }

    /**
     * 同步货小易订单状态
     */
    public void syncWarehouseOrderStatus() {
        syncWarehouseInitOrderStatus();
        syncWareHouseWaitProcessOrderStatus();
        syncWareHouseWaitPackOrderStatus();
    }

    /**
     * 同步货代打包费用
     */
    public void syncWarehousePackageAmount() {
        log.info("开始同步货代打包费用");

        List<OrderIncomeDto> orderItemDtoList = ObjectUtil.isNull(orderInfoService.getHaveNoWarehousePackageAmountOrder()) ? new ArrayList<>() : orderInfoService.getHaveNoWarehousePackageAmountOrder();
        for (OrderIncomeDto orderIncomeDto : orderItemDtoList) {
            try {
                BigDecimal warehousePackageAmount = hxyOrderService.getOrderWarehousePackageAmount(orderIncomeDto.getOrderSn());
                OrderIncomeDto orderIncomeDtoNew = new OrderIncomeDto();
                orderIncomeDtoNew.setOrderSn(orderIncomeDto.getOrderSn());
                orderIncomeDtoNew.setWarehousePackageAmount(warehousePackageAmount);
                orderInfoService.updateOrderIncome(orderIncomeDtoNew);
                orderInfoService.updatePurchasePrice(orderIncomeDto.getOrderSn());
            }catch (IOException e) {
                log.error("获取货小易{}订单失败", orderIncomeDto.getOrderSn(), e);
            }
        }

        log.info("结束同步货代打包费用");
    }

    /**
     * 同步货小易仓库待处理的订单状态
     */
    public void syncWarehouseInitOrderStatus() {
        log.info("开始同步货小易仓库待处理的订单状态");

        List<OrderDto> orderDtoList = orderInfoService.getWarehouseInitOrder();
        for (OrderDto orderDto : orderDtoList) {
            OrderItemInfoDto orderItemInfoDto = null;
            try {
                orderItemInfoDto = hxyOrderService.getOrderItemByOrderSn(orderDto.getOrderSn());
                if (orderItemInfoDto.getWarehouseProcessStatus() != null) {
                    orderInfoService.addOrderItemGoodsOrHeadPackageInfo(orderItemInfoDto);
                }else {
                    log.info("订单{}未出货", orderDto.getOrderSn());
                }
            } catch (IOException e) {
                log.error("获取货小易{}订单失败", orderDto.getOrderSn(), e);
            }
        }

        log.info("结束同步货小易仓库待处理的订单状态");
    }

    /**
     * 同步货小易仓库处理中的订单状态
     */
    public void syncWareHouseWaitProcessOrderStatus() {
        log.info("开始同步货小易仓库处理中的订单状态");

        List<OrderDto> orderDtoList = orderInfoService.getWareHouseWaitProcessOrder();
        for (OrderDto orderDto : orderDtoList) {
            OrderItemInfoDto orderItemInfoDto = null;
            try {
                orderItemInfoDto = hxyOrderService.getOrderItemByOrderSn(orderDto.getOrderSn());
                if (!orderItemInfoDto.getWarehouseProcessStatus().equals(orderDto.getWarehouseProcessStatus())) {
                    orderInfoService.updateOrderItemGoodsOrHeadPackageInfo(orderItemInfoDto);
                }else {
                    log.info("订单{}未更新", orderDto.getOrderSn());
                }
            } catch (IOException e) {
                log.error("获取货小易{}订单失败", orderDto.getOrderSn(), e);
            }
        }

        log.info("结束同步货小易仓库处理中的订单状态");
    }

    /**
     * 同步货小易仓库待打包的订单状态
     */
    public void syncWareHouseWaitPackOrderStatus() {
        log.info("开始同步货小易仓库待打包的订单状态");

        List<OrderDto> orderDtoList = orderInfoService.getWareHouseWaitPackOrder();
        for (OrderDto orderDto : orderDtoList) {
            OrderItemInfoDto orderItemInfoDto = null;
            try {
                orderItemInfoDto = hxyOrderService.getOrderItemByOrderSn(orderDto.getOrderSn());
                if (!orderItemInfoDto.getWarehouseProcessStatus().equals(orderDto.getWarehouseProcessStatus())) {
                    orderInfoService.updateOrderItemGoodsOrHeadPackageInfo(orderItemInfoDto);
                }else {
                    log.info("订单{}未更新", orderDto.getOrderSn());
                }
            } catch (IOException e) {
                log.error("获取货小易{}订单失败", orderDto.getOrderSn(), e);
            }
        }

        log.info("结束同步货小易仓库待打包的订单状态");
    }

    public void syncWareGoods() throws IOException {
        log.info("开始同步货小易仓库货物信息");

        List<SupplyWarehouseGoodsDto> supplyWarehouseGoodsDtoList = hxyGoodsService.getGoodsSkuList();
        supplyWarehouseService.updateOrAddWarehouseGoods(supplyWarehouseGoodsDtoList);

        log.info("结束同步货小易仓库货物信息");
    }
}
